package demo4;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class test4
{
    List<Drink> drinks = new ArrayList<Drink>();
    {
        Drink drink1 = new Drink("牛奶",8,160);
        Drink drink2 = new Drink("水",4,24);
        Drink drink3 = new Drink("五粮液",2,4000);
        Drink drink4 = new Drink("可乐",6,108);
        Drink drink5 = new Drink("茅台",1,4000);
        Collections.addAll(drinks,drink1,drink2,drink3,drink4,drink5);
    }
    @Test
    public void func1()
    {
       initArr();
       func2();
    }

    private void func2()
    {   int sumValue = 0, sumIcanWeight = 10;
        for(Drink drink:drinks)
        {
            if(sumIcanWeight==0)break;
           int ActualTheWeight=MIN(sumIcanWeight,drink.getWeight());
           sumIcanWeight-=ActualTheWeight;
           sumValue+=drink.getOne_value()*ActualTheWeight;
            System.out.println("拿了" + drink.getName() + ActualTheWeight + "份");
        }
        System.out.println("总价为：" + sumValue);
    }

    private int MIN(int a,int b)
    {
        return a>b?b:a;
    }

    private void initArr()
    {

        Collections.sort(drinks, new Comparator<Drink>() {
            @Override
            public int compare(Drink o1, Drink o2) {
                return Double.compare(o2.getOne_value(),o1.getOne_value());
            }
        });
//        System.out.println("1231231");
//        for(Drink drink:drinks)
//        {
//            System.out.println(drink);
//        }
//        System.out.println("222222222222222222");
    }

}
